package sweetcamera;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.MainActivity;
import com.github.postsanf.yinian.activity.MicAddActivity;
import com.github.postsanf.yinian.activity.PailideIntroduceActivity;
import com.github.postsanf.yinian.activity.TimeGoodsAlbumActivity;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.utils.FileUtils;
import com.github.postsanf.yinian.utils.SpUtils;

import java.io.File;

import sweetcamera.album.AlbumHelper;
import sweetcamera.util.FileUtil;
import sweetcamera.widget.SquareCameraContainer;

public class CameraActivity extends Activity {
    public static final String TAG = "CameraActivity";
    private CameraManager mCameraManager;
    private TextView m_tvFlashLight, m_tvCameraDireation;
    private SquareCameraContainer mCameraContainer;
    private ImageButton m_ibRecentPic;
    private int mFinishCount = 2;   //finish计数   当动画和异步任务都结束的时候  再调用finish方法
    AlbumHelper helper;
    private Handler handler = new Handler();
    public static boolean IsSaoMiao = false;
    public static String groupid = "";
    private LinearLayout camera_pailide_buttom;
    public static boolean fromfrist = false;
    public static int fromfristgid = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!SpUtils.getBoolean(CameraActivity.this, "PailideIntroduce")) {
            startActivity(new Intent(CameraActivity.this, PailideIntroduceActivity.class));
        }
        SpUtils.put(CameraActivity.this, "PailideIntroduce", true);
        setContentView(R.layout.activity_camera);
        registerBoradcastReceiver();
        FileUtil.initFolder();
        mCameraManager = CameraManager.getInstance(this);
        helper = AlbumHelper.getHelper();
        helper.init(getApplicationContext());
        BaseApplication application = (BaseApplication) getApplication();
        groupid = String.valueOf(application.getCurGid());
        if (getIntent() != null && getIntent().getBooleanExtra("IsSaoMiao", false)) {
            IsSaoMiao = true;
        }
        if (getIntent() != null) {
            fromfrist = getIntent().getBooleanExtra("fromfrist", false);
            fromfristgid = getIntent().getIntExtra("gid", 0);
        }
        initView();
        initData();
        initListener();
    }

    private void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actionCloseAlbum);
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.actionCloseAlbum:
                    finish();
                    break;
            }
        }
    };


    void initView() {
        findViewById(R.id.camera_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        m_tvFlashLight = (TextView) findViewById(R.id.tv_flashlight);
//        m_tvCameraDireation = (TextView) findViewById(R.id.tv_camera_direction);
        mCameraContainer = (SquareCameraContainer) findViewById(R.id.cameraContainer);
//        m_ibRecentPic = (ImageButton) findViewById(R.id.ib_recentpic);
        camera_pailide_buttom = (LinearLayout) findViewById(R.id.camera_pailide_buttom);
        if (!IsSaoMiao) {
            camera_pailide_buttom.setVisibility(View.VISIBLE);
        } else {
            camera_pailide_buttom.setVisibility(View.GONE);
        }
        findViewById(R.id.ll_camera_album).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.goodsid = 101;//拍立得选图
                startActivity(new Intent(CameraActivity.this, TimeGoodsAlbumActivity.class));
            }
        });
        findViewById(R.id.camera_course).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(CameraActivity.this, PailideIntroduceActivity.class));
            }
        });
    }

    void initData() {
        mCameraManager.bindOptionMenuView(m_tvFlashLight, m_tvCameraDireation);
//        mCameraContainer.setImagePath(getIntent().getStringExtra(PATH_OUTIMG));
        mCameraContainer.bindActivity(CameraActivity.this);
        //todo  获取系统相册中的一张相片
//        List<ImageItem> list = helper.getImagesList();
//        if (list != null && list.size() != 0) {
//            m_ibRecentPic.setImageBitmap(BitmapUtils.createCaptureBitmap(list.get(0).imagePath));
//            m_ibRecentPic.setScaleType(ImageView.ScaleType.CENTER_CROP);
//            m_ibRecentPic.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    //跳转到系统相册
//                    Intent intent = new Intent(Intent.ACTION_DEFAULT,
//                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//                    startActivity(intent);
//                }
//            });
//        } else {
        //设置默认图片
//            m_ibRecentPic.setImageResource(R.drawable.selector_camera_icon_album);
//        }
    }

    void initListener() {
        if (mCameraManager.canSwitch()) {
//            m_tvCameraDireation.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    m_tvCameraDireation.setClickable(false);
//                    mCameraContainer.switchCamera();
//                    //500ms后才能再次点击
//                    handler.postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            m_tvCameraDireation.setClickable(true);
//                        }
//                    }, 500);
//                }
//            });
        }
        m_tvFlashLight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCameraContainer.switchFlashMode();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mCameraContainer != null) {
            mCameraContainer.onStart();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mCameraContainer != null) {
            mCameraContainer.onStop();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCameraManager.unbinding();
        mCameraManager.releaseActivityCamera();
        unregisterReceiver(mBroadcastReceiver);
        clearAll();
        new Thread() {
            @Override
            public void run() {
                super.run();
                if (new File(FileUtil.getCameraImageFolder()).exists()) {
                    FileUtils.deleteDir(FileUtil.getCameraImageFolder());
                }
            }
        }.start();
    }

    private void clearAll() {
        CameraActivity.IsSaoMiao = false;
        CameraPublishActivity.content = "";
        MicAddActivity.mMp3FileName = "";
        MainActivity.mp3IsExist = false;
        MainActivity.micUrl = "";
        MainActivity.micpath = "";
        SquareCameraContainer.mImagePath = "";
        CameraPublishActivity.totalNum = "";
        CameraPublishActivity.totalMoney = "";
        SelectedImg.mSelectedImage.clear();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        //在创建前  释放相机
    }

    /**
     * 对一些参数重置
     */
    public void rest() {
        mFinishCount = 2;
    }

    /**
     * 退出按钮点击
     */
    public void onExitClicked(View view) {
        onBackPressed();
    }

    /**
     * 照相按钮点击
     */
    public void onTakePhotoClicked(View view) {
        mCameraContainer.takePicture();
    }

    /**
     * 提交finish任务  进行计数  都在main Thread
     */

    /**
     * 照完照片 提交
     */
    public void postTakePhoto() {
//        mFinishCount--;
//        if (mFinishCount < 0) mFinishCount = 2;
//        if (mFinishCount == 0) {
//            setResult(RESULT_OK);
//            finish();
//        }
//        mCameraManager.releaseActivityCamera();
//        Toast.makeText(this, "take photo", Toast.LENGTH_SHORT).show();

    }
}
